﻿using System;
using Budget.Core.Data;
using Budget.Core.Exceptions;
using NHibernate;
using NHibernate.Linq;

namespace Budget.Data
{
    public class Repository<T> : IRepository<T>
    {
        private readonly ISession session;

        public Repository(ISession session)
        {
            Check.Require<ArgumentNullException>(session != null, "session is null!");

            this.session = session;
        }

        public void Remove(T entity)
        {
            session.Delete(entity);
        }

        public void SaveOrUpdate(T entity)
        {
            session.SaveOrUpdate(entity);
        }

        public IFinder<T> Find
        {
            get
            {
                return new Finder<T>(session.Query<T>());
            }
        }
    }
}